package com.edgclub.edg.uc;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.GridView;

/**
 * 不能滑动的gridview 用于嵌入到scrollview之中
 * 
 * @author 陈坤彬
 *
 */
public class UnableScrollGridView extends GridView {

	public OnTouchBlankPositionListener mTouchBlankPosListener;

	public UnableScrollGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public UnableScrollGridView(Context context) {
		super(context);
	}

	public void setOnTouchBlankPositionListener(
			OnTouchBlankPositionListener mTouchBlankPosListener) {
		this.mTouchBlankPosListener = mTouchBlankPosListener;
	}

	@Override
	 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	        int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
	        super.onMeasure(widthMeasureSpec, mExpandSpec);
	    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (mTouchBlankPosListener != null) {
			if (!isEnabled()) {
				return isClickable() || isLongClickable();
			}

			// 这里监听手指的抬起事件,如果点击的不是有效的item那么就是空白的地方
			if (event.getActionMasked() == MotionEvent.ACTION_UP) {
				final int motionPosition = pointToPosition((int) event.getX(),
						(int) event.getY());
				if (motionPosition == GridView.INVALID_POSITION) {
					return mTouchBlankPosListener.onTouchBlankPosition();
				}
			}
		}

		return super.onTouchEvent(event);
	}

	public interface OnTouchBlankPositionListener {
		/**
		 * 
		 * @return 是否要终止事件的路由
		 */
		boolean onTouchBlankPosition();
	}

}
